import pyglet
from pyglet.gui import PushButton
from pyglet.window import key

# import SceneManager
from BaseScene import BaseScene
from SaveLabel import SaveLabel

class SaveScene(BaseScene):
    """存档场景"""

    def __init__(self, scene_manager, pointer):
        super().__init__(scene_manager)

        # 创建存档界面元素
        #self.background = shapes.Rectangle(0, 0, self.window.width, self.window.height,
        #
        #                                   color=(255, 255, 255), batch=self.batch)
        self.savebackground=pyglet.sprite.Sprite(self.pic_resources["savebg"],x=self.scene_manager.get_cx(),y=self.scene_manager.get_cy(),batch=self.batch,group=pyglet.graphics.Group(1))
        self.play_music("save")

        self.title = pyglet.text.Label('1 2 3 4 5 6 7 8 9', font_name='SimHei', font_size=60,
                                       x=450+self.scene_manager.get_cx(), y=980+self.scene_manager.get_cy(),
                                       anchor_x='center', batch=self.batch,group=self.group3)
        self.page=1
        self.page_block=pyglet.sprite.Sprite(self.pic_resources["pageblock_0"],x=100,y=1000,batch=self.batch,group=self.group2)
        self.page1 = PushButton(100, 1000, self.pic_resources["pageblock_0"], self.pic_resources["pageblock_1"],
                                batch=self.batch,group=self.group4)
        self.page2 = PushButton(180, 1000, self.pic_resources["pageblock_0"], self.pic_resources["pageblock_1"],
                                batch=self.batch, group=self.group4)
        self.page3 = PushButton(260, 1000, self.pic_resources["pageblock_0"], self.pic_resources["pageblock_1"],
                                batch=self.batch, group=self.group4)
        self.page4 = PushButton(340, 1000, self.pic_resources["pageblock_0"], self.pic_resources["pageblock_1"],
                                batch=self.batch, group=self.group4)
        self.page5 = PushButton(420, 1000, self.pic_resources["pageblock_0"], self.pic_resources["pageblock_1"],
                                batch=self.batch, group=self.group4)
        self.page6 = PushButton(500, 1000, self.pic_resources["pageblock_0"], self.pic_resources["pageblock_1"],
                                batch=self.batch, group=self.group4)
        self.page7 = PushButton(580, 1000, self.pic_resources["pageblock_0"], self.pic_resources["pageblock_1"],
                                batch=self.batch, group=self.group4)
        self.page8 = PushButton(660, 1000, self.pic_resources["pageblock_0"], self.pic_resources["pageblock_1"],
                                batch=self.batch, group=self.group4)
        self.page9 = PushButton(740, 1000, self.pic_resources["pageblock_0"], self.pic_resources["pageblock_1"],
                                batch=self.batch, group=self.group4)
        self.page1.set_handler('on_press', self.p1handler)
        self.page2.set_handler('on_press', self.p2handler)
        self.page3.set_handler('on_press', self.p3handler)
        self.page4.set_handler('on_press', self.p4handler)
        self.page5.set_handler('on_press', self.p5handler)
        self.page6.set_handler('on_press', self.p6handler)
        self.page7.set_handler('on_press', self.p7handler)
        self.page8.set_handler('on_press', self.p8handler)
        self.page9.set_handler('on_press', self.p9handler)
        self.buttons.append(self.page1)
        self.buttons.append(self.page2)
        self.buttons.append(self.page3)
        self.buttons.append(self.page4)
        self.buttons.append(self.page5)
        self.buttons.append(self.page6)
        self.buttons.append(self.page7)
        self.buttons.append(self.page8)
        self.buttons.append(self.page9)



        self.pointer=pointer

        ####存档场景主代码

        #self.save_button = PushButton(button_x, 200, pressed_img, depressed_img)
        #self.save_button.set_handler('on_release', self.on_save_pressed)
        # self.save01_btn = PushButton(100, 200, pressed_img, depressed_img,batch=self.batch,group=self.group3)
        # self.load01_btn = PushButton(100, 400, pressed_img, depressed_img,batch=self.batch,group=self.group3)
        # self.save01_btn.set_handler('on_release',self.save01_btn_handler)
        # self.load01_btn.set_handler('on_release', self.load01_btn_handler)
        # self.buttons.append(self.save01_btn)
        # self.buttons.append(self.load01_btn)

        self.savelabel1 = SaveLabel(self.scene_manager.get_cx() + 100, self.scene_manager.get_cy() + 700, (self.page - 1) + 1, self.batch, self.pic_resources,
                                    self.pointer, self.scene_manager, self.window,self.buttons)
        self.savelabel2 = SaveLabel(self.scene_manager.get_cx() + 800, self.scene_manager.get_cy() + 700, (self.page - 1) + 2, self.batch, self.pic_resources,
                                    self.pointer, self.scene_manager, self.window,self.buttons)
        self.savelabel3 = SaveLabel(self.scene_manager.get_cx() + 100, self.scene_manager.get_cy() + 400, (self.page - 1) + 3, self.batch, self.pic_resources,
                                    self.pointer, self.scene_manager, self.window,self.buttons)
        self.savelabel4 = SaveLabel(self.scene_manager.get_cx() + 800, self.scene_manager.get_cy() + 400, (self.page - 1) + 4, self.batch, self.pic_resources,
                                    self.pointer, self.scene_manager, self.window,self.buttons)
        self.savelabel5 = SaveLabel(self.scene_manager.get_cx() + 100, self.scene_manager.get_cy() + 100, (self.page - 1) + 5, self.batch, self.pic_resources,
                                    self.pointer, self.scene_manager, self.window,self.buttons)
        self.savelabel6 = SaveLabel(self.scene_manager.get_cx() + 800, self.scene_manager.get_cy() + 100, (self.page - 1) + 6, self.batch, self.pic_resources,
                                    self.pointer, self.scene_manager, self.window,self.buttons)
        self.savelabels=[self.savelabel1,self.savelabel2,self.savelabel3,self.savelabel4,self.savelabel5,self.savelabel6]



    def update_page(self):
        self.page_block.x = 100 + (self.page - 1) * 80
        print("ok")
        print(self.page)
        i=1
        for label in self.savelabels:
            label.update_num((self.page-1)*6+i)
            i+=1

    def p1handler(self,button):
        self.page = 1
        self.update_page()
    def p2handler(self,button):
        print("okhandle\n")
        self.page = 2
        self.update_page()
    def p3handler(self,button):
        self.page = 3
        self.update_page()
    def p4handler(self,button):
        self.page = 4
        self.update_page()
    def p5handler(self,button):
        self.page = 5
        self.update_page()
    def p6handler(self,button):
        self.page = 6
        self.update_page()
    def p7handler(self,button):
        self.page = 7
        self.update_page()
    def p8handler(self,button):
        self.page = 8
        self.update_page()
    def p9handler(self,button):
        self.page = 9
        self.update_page()

    # def save01_btn_handler(self, button):
    #     output_saves("save01",str(self.pointer))
    #
    # def load01_btn_handler(self, button):
    #     p = read_saves('save01')
    #     if p != -1:
    #         game_scene = GameScene(self.scene_manager, p)
    #         self.scene_manager.switch_scene(game_scene)
        #################################################################错误处理还没写·





    def on_draw(self):
        """绘制存档场景"""
        self.window.clear()
        self.batch.draw()

    def on_key_press(self, symbol, modifiers):
        """键盘事件处理"""
        if symbol == key.ESCAPE:
            # 返回开始场景
            # start_scene = StartScene(self.scene_manager)
            for label in self.savelabels:
                label.destroy()
            self.scene_manager.switch_scene("start")
